package nl.haemers.galleries
{
	import flash.display.Sprite;
	import flash.events.Event;
	import nl.haemers.galleries.galleryitem.BaseItem;
	
	import nl.haemers.base.BaseSprite;
	import nl.haemers.events.SpriteEvent;
	
	import nl.haemers.galleries.galleryitem.IItem;
	/**
	 * @author Jonathan Haemers
	  */
	public class BaseGallery extends BaseSprite
	{
		protected var _items					:Array;
		
		protected var _selectedItem				:BaseItem;
		protected var previousItem				:BaseItem;
		
		public function BaseGallery() 
		{
			super();
		}
		
		/** Public methods **/
		public function next() : void
		{
			previousItem = _selectedItem;
			_selectedItem = _selectedItem.next as BaseItem;
			
			if (!contains(_selectedItem))
			{
				addChild(_selectedItem);
			}
			
			previousItem.hide();
			_selectedItem.show();
		}
		
		public function previous() : void
		{
			previousItem = _selectedItem;
			_selectedItem = _selectedItem.previous as BaseItem;
			if (!contains(_selectedItem))
			{
				addChild(_selectedItem);
			}
			
			previousItem.hide();
			_selectedItem.show();
		}
		
		public function addItem(item:IItem) : void
		{
			if (_items == null)
			{
				_items = new Array();
			}
			_items.push(item);
		}
		
		/** Getters & Setters **/
		public function get items() : Array
		{ 
			return _items;
		}
		public function set items( value:Array ) : void 
		{
			_items = value;
		}
		
		public function get selectedItem() : IItem
		{ 
			return _selectedItem as IItem;
		}
		
		/** Protected methods **/
		override protected function init( e:Event = null ) : void
		{
			super.init(e);
			
			startup();
		}
		
		protected function startup() : void
		{
			configListeners();
		}
		
		protected function createGallery() : void
		{
		}
		
		protected function configListeners() : void
		{
			_selectedItem.addEventListener(SpriteEvent.SHOW_COMPLETE, showItemHandler);
			previousItem.addEventListener(SpriteEvent.HIDE_COMPLETE, hideItemCompleteHandler);
		}
		
		protected function showItemHandler(e:SpriteEvent) : void
		{
			trace(e);
		}
		
		protected function hideItemCompleteHandler(e:SpriteEvent) : void
		{
			trace("BaseGallery" + e);
			var item:BaseItem = e.currentTarget as BaseItem;
			if (contains(item))
			{
				removeChild(item);
				item = null;
			}
		}
	}

}